'''
Created on Mar 1, 2014

@author: Hovanes Egiyan
'''

#import os
#import sys


class subsystem(object):
    '''
    Class to handle a node in the detector hierarchy 
    '''
    classID = 0
    


    def __init__(self, parent=None, compType="", compName="", chanidArg="NULL"):
        '''
        Constructor
        '''
        self.name       = compName          # name in the detector hierarchy tree 
        self.type       = compType          # type in the detector hierarchy tree
        self.chanid     = chanidArg         # chanid in the detector hierarchy tree
        self.mtime      = None              # mtime (modification time) in the detector hierarchy tree

        self.children   = []                # A list with the children of this object taken from the detector hierarchy DB
        self.parentObj  = parent            # Reference to the parent object of the same class as self      
           
        self.id         = self.incrementID()    # id in the detector hierarchy DB
        self.parent_id  = "NULL"
        if( parent != None ) :
            self.parent_id  = parent.id             # paprent_id in the detector hierarchy tree 
    
    
    def makeSQL(self, fileHandle):
        '''
        Create SQL string and append it into the output file
        '''
        sql_string = "INSERT INTO Detector_Hierarchy ( id, parent_id, name, type, chanid ) VALUES ( %s, %s, '%s', '%s', %s );\n" %(self.id, self.parent_id, self.name,self.type, self.chanid)
        
        fileHandle.write(sql_string)
        return
    
    
    def incrementID(self):
        subsystem.classID += 1 
        return subsystem.classID

    